package cz.boxdesign.library.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cz.boxdesign.library.entity.Department;
import cz.boxdesign.library.service.BookService;
import cz.boxdesign.library.service.DepartmentService;
import cz.boxdesign.library.web.validation.DepartmentValidator;

@Controller
@RequestMapping("/department")
public class DepartmentController {

	@Autowired
	@Qualifier("departmentService")
	private DepartmentService departmentService;

	@Autowired
	@Qualifier("knihaService")
	private BookService bookService;

	@InitBinder
	public void initModel(WebDataBinder binder) {
		Object targer = binder.getTarget();
		if (targer instanceof Department) {
			if (binder.getValidator() == null) {
				binder.setValidator(new DepartmentValidator());
			}
		}
	}

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView showList() {
		ModelAndView model = new ModelAndView("department/showList");
		List<Department> oddeleniList = departmentService.getAll();
		model.addObject("departmentList", oddeleniList);
		return model;
	}

	@RequestMapping(value = "/show", method = RequestMethod.GET)
	public ModelAndView show(@RequestParam("departmentId") Integer departmentId) {
		ModelAndView model = new ModelAndView("department/show");
		model.addObject("department", departmentService.getById(departmentId));
		model.addObject("knihaList", bookService.getByClause("and id_o=" + departmentId));
		return model;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam("departmentId") Integer departmentId) {
		ModelAndView model = new ModelAndView("department/edit");
		model.addObject("department", departmentService.getById(departmentId));
		return model;
	}
	
	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public ModelAndView edit() {
		ModelAndView model = new ModelAndView("department/new");
		model.addObject("department", new Department());
		return model;
	}
	
	@RequestMapping(value = "/submit", method = RequestMethod.POST)
	public ModelAndView submit(@RequestParam("departmentId") Integer departmentId,Department view ,BindingResult error){
		ModelAndView model = new ModelAndView();
		if(!error.hasErrors()){
			if(departmentId != null){
				Department dep = departmentService.getById(departmentId);
				dep.setNazev(view.getNazev());
				departmentService.updateEntity(dep);
				return new ModelAndView(new RedirectView("show.do?departmentId="+dep.getId()));
			}else{
				view.setPocetKnih(0);
				departmentService.insertEntity(view);
				Department dep = departmentService.getByClause("and name='"+view.getNazev()+"'").get(0);
				return new ModelAndView(new RedirectView("show.do?departmentId="+dep.getId()));
			}
		}
		//TODO
		return null;
	}

}
